package com.xiaotu.spider.weibo.pipeline;

import org.springframework.stereotype.Component;

import com.xiaotu.common.model.StarWeiboModel;
import com.xiaotu.common.model.TVWeiboModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.spider.APipeline;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

/**
 * @类名 WeiboPipeline
 * @日期 2017年2月10日
 * @作者 高海军
 * @功能 微博数据pipeline
 */
@Component("WeiboPipeline")
public class WeiboPipeline extends APipeline
{
	@Override
	protected void doProcess(ResultItems resultItems, Task task)
	{
		TVWeiboModel tvWeiboModel = resultItems
				.get(TVWeiboModel.class.getSimpleName());// 作品微博数据
		if (tvWeiboModel != null)
		{
			String key = getTVKey();
			cacheHandler.hSet(key, tvWeiboModel.getTvId(), tvWeiboModel);
			cacheHandler.setCacheTimeout(key);
		}
		
		StarWeiboModel starWeiboModel = resultItems
				.get(StarWeiboModel.class.getSimpleName());// 艺人微博数据
		if (starWeiboModel != null)
		{
			String key = getStarKey();
			cacheHandler.hSet(key, starWeiboModel.getStarId(), starWeiboModel);
			cacheHandler.setCacheTimeout(key);
		}
	}
	
	protected String getTVKey()
	{
		return DataRedisKeyUtils
				.getKey(Constants.DataRedisKey.TV_Weibo_YYYYMMDD);
	}
	
	protected String getStarKey()
	{
		return DataRedisKeyUtils
				.getKey(Constants.DataRedisKey.Star_Weibo_YYYYMMDD);
	}
}